package org.dromara.frame.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.json.JSONUtil;
import lombok.extern.log4j.Log4j2;
import org.apache.dubbo.config.annotation.DubboReference;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.web.core.BaseController;
import org.dromara.frame.api.RemoteFrameService;
import org.dromara.frame.api.domain.FrameImage;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;

/**
 * 抽帧服务
 * @author samuel
 * @date    2024-03-25 10:55:00
 */
@Log4j2
@RestController
@RequestMapping("extraction")
public class FrameController extends BaseController {

    @DubboReference
    private RemoteFrameService remoteFrameService;

    @SaIgnore
    @PostMapping("/{deviceIds}")
    public R<List<FrameImage>> extraction(@PathVariable("deviceIds") String[] deviceIds) throws ExecutionException, InterruptedException, TimeoutException {
        log.info("开始抽帧服务， 抽帧设备：{}", JSONUtil.toJsonStr(deviceIds));
        if (null == deviceIds || deviceIds.length == 0) {
            throw new ServiceException("请选择需要抽帧的设备");
        }
        List<Long> ids = new ArrayList<>();
        for (String deviceId : deviceIds) {
            ids.add(Long.parseLong(deviceId));
        }
        List<FrameImage> frameImages = remoteFrameService.extraction(ids);

        return R.ok("抽帧成功", frameImages);
    }

}
